package main.java.cn.touna.jss.modules.third.gzt.util;


import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import cn.touna.jss.common.util.Base64;

public class GuoZhengTongUtil {

    // 密钥
    private final static String secretKey = "12345678";

    // 向量
    private final static String iv = "12345678";

    // 加解密统一使用的编码方式
    private final static String encoding = "GBK";

    /**
     * 加密算法
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static String encode(String edata) throws Exception {
        byte[] data;
        data = edata.getBytes(encoding);
        DESKeySpec dks = new DESKeySpec(secretKey.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key的长度不能够小于8位字节
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());// 向量
        AlgorithmParameterSpec paramSpec = ips;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data);
        return Base64.encode(bytes);
    }

    /**
     * 解密算法
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static String decode(String edata) throws Exception {
        byte[] data;
        data = Base64.decode(edata);
        try {
            DESKeySpec dks = new DESKeySpec(secretKey.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key 的长度不能够小于 8 位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            AlgorithmParameterSpec paramSpec = ips;
            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
            return new String(cipher.doFinal(data), encoding);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    public static void main(String[] args) throws Exception {
        // System.out.println("abc加密结果:" + encode("abc"));
        // System.out.println("ABC加密结果:" + encode("ABC"));
        // System.out.println("中国人加密结果:" + encode("中国人"));
        // System.out.println("在那遥远的地方:" + encode("在那遥远的地方"));
        System.out.println("解密:"
                + decode("S078JKKtSvAMWap/sIGpd4eiphXoB0MB8EfFuFogsz8RLhrqnYxuAI8jchhX2zrYlCcVJcbeIfkwP7h6A+13QRIPDjUZPyuG6UtnbQ6iIeZ3kbVstT9zQlcDUPMVjAD4JHVZ2lWCPqykPdm+SBwc7OSYGsS5Q8mEyAjGgMXHAVxlQeLauYa356CSOcsB/pjUMYnx0gIkq6UbBaww+hoQmEhAruzflpFx55lURPRvl3M9zYkTdrbA8n1t6jQMFFGTw44pO3fzJr4Hy8y3s3kpzWOtS8QOvx8mCQdNj4z/UbTQdfjUSd3vVxOYXUMFYyAJEOnj65QXdXn/JMqnchwyrcAKCCbdFmQvLjBgyYcj4RJIHKNTRIE/6c17/FIeceHHDg3rnVsPWNnkL0vvUlpGgdOSLHxpqIMJNBudOpVKJH3JacdGP+o8P0jy0ani4At16yyIoNGDz0o1w1ZbA16lOhFBcfHD8DsXfrdPcI3wbsBAFqaFUTwK5BZZ3eV7MR5kBjibweJUt5HAbvvEOYlnzdY/Ovn2Whd0Ohm43HjVgypZK5jIyVbC4FLEYRdGikV1+UGhKpXqT5Oz1b5cv2VZSahXC4Z8SN12ZIUYAMP8ds2X59v9+PvfNQ4N2ZPMsO0RXVSzJYKpIFTiSGe0y0trknvLgpszR9GvOxH72GeNHMGnfEpdjwnnxJYd3QNNviJme7X6lCo82ni1T5nVvRDyOsS6Hk/nHI11Yp9qOdtbO9r9TAhqB9jZ7HAKvRFjx8zyhxjcobf5CEMM9t3tNWGE/VZHWVPeJHYsHDlI7+nH2j5cQZ2FQKDRuKM6JOjbUfbQnsOc8q4WD5VHK4aGehOmlTiLNPXGlAHbef/1iqTpup8c7+y8MjxVx8MMdKImRYumUmSoAC85563OBpVj8FcBqitUEfWoRht68Gt78S+PplWO+5BdW+twpPoHRN0Qnu1q8ZETr7hOm778p42e+qyhq2+jLeq++TFe3y7yk9L4BVXCcHnEw4tw0t+uYJGIIkuZ"));
    }
}
